package com.sangcomz.fishbun.ui.album.mvp;

import com.sangcomz.fishbun.ui.album.model.Album;
import com.sangcomz.fishbun.ui.album.model.AlbumViewData;
import com.sangcomz.fishbun.ui.album.model.repository.AlbumRepository;
import com.sangcomz.fishbun.ui.album.AlbumContract;
import com.sangcomz.fishbun.util.UiHandler;
import com.sangcomz.fishbun.util.future.CallableFutureTask;
import com.sangcomz.fishbun.util.future.CallableFutureTask.FutureCallback;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class AlbumPresenter implements AlbumContract.Presenter {

    private HiLogLabel TAG = new HiLogLabel(HiLog.ERROR, 0x123, "AlbumPresenter");

    private AlbumContract.View albumView;
    private AlbumRepository albumRepository;
    private UiHandler uiHandler;

    private CallableFutureTask<List<Album>> albumListFuture = null;

    public AlbumPresenter(AlbumContract.View albumView, AlbumRepository albumRepository, UiHandler uiHandler) {
        this.albumView = albumView;
        this.albumRepository = albumRepository;
        this.uiHandler = uiHandler;
    }

    @Override
    public void loadAlbumList() {

        HiLog.error(TAG, "AlbumPresenter load album list start!");

        albumListFuture = albumRepository.getAlbumList();

        albumListFuture.execute(new FutureCallback<List<Album>>() {
            @Override
            public void onSuccess(List<Album> result) {
                uiHandler.run(new UiHandler.RunUIThread() {
                    @Override
                    public void run() {
                        if (!result.isEmpty()) {
                            changeToolbarTitle();
                            try {
                                albumView.showAlbumList(
                                        albumListFuture.get(),
                                        albumRepository.getImageAdapter(),
                                        albumRepository.getAlbumViewData()
                                );
                            } catch (ExecutionException | InterruptedException e) {
                                e.printStackTrace();
                            }
                        } else {
                            albumView.showEmptyView();
                        }
                    }
                });
            }
        });
    }

    @Override
    public void takePicture() {
        albumView.takePicture(albumRepository.getDefaultSavePath());
    }

    @Override
    public void getDesignViewData() {
        AlbumViewData viewData = albumRepository.getAlbumViewData();
        albumView.setRecyclerView(viewData);
        albumView.setToolBar(viewData);
        changeToolbarTitle();
    }


    @Override
    public void onResume() {
        albumView.setRecyclerViewSpanCount(albumRepository.getAlbumViewData());
    }

    @Override
    public void finish() {
        albumView.finishActivityWithResult(albumRepository.getSelectedImageList());
    }

    @Override
    public void refreshAlbumItem(int position, ArrayList<Uri> addedPathList) {
        changeToolbarTitle();
        if (addedPathList.size() > 0) {
            if (position == 0) loadAlbumList();
            else albumView.refreshAlbumItem(position, addedPathList);
        }
    }

    @Override
    public String getPathDir() {
        return albumRepository.getDefaultSavePath();
    }


    @Override
    public void release() {
        if (albumListFuture != null){
            albumListFuture.cancel(true);
        }
    }

    private void changeToolbarTitle() {
        albumView.changeToolbarTitle(
                albumRepository.getSelectedImageList().size(),
                albumRepository.getAlbumViewData()
        );
    }

    @Override
    public void getAlbumMenuViewData(CallBack callback) {
        callback.Call(albumRepository.getAlbumMenuViewData());
    }

    @Override
    public void onClickMenuDone() {
        int selectedCount = albumRepository.getSelectedImageList().size();

        if (selectedCount == 0){
            albumView.showNothingSelectedMessage(albumRepository.getMessageNotingSelected());
        }else if (selectedCount < albumRepository.getMinCount()){
            albumView.showMinimumImageMessage(albumRepository.getMinCount());
        }else {
            finish();
        }
    }

    @Override
    public void onSuccessTakePicture() {
        albumView.saveImage();
        albumView.scanAndRefresh();
    }

    static class Object {
        private final String defaultDir = "/Camera";
    }
}